#!/bin/sh

echo '/***************************************/'
echo '/* Déplacement dans le répertoire bin/ */'
echo '/***************************************/'
cd bin
echo '/***************************************/'
echo '/*           start registry            */'
echo '/***************************************/'
rmiregistry &
pid_registry=$!
echo 'Registry is running...'
echo 'PID registry is : ' $pid_registry

echo ''
echo '/***************************************/'
echo '/*        Création des noeuds          */'
echo '/***************************************/'
echo ''

# test creation d'un noeud incorrect
echo '*** Creation du noeud incorrect ***'
java -Djava.security.policy=../mypolicy rmi.tree.SiteImpl aaaaaa > ../PrintNoeud/testCreation_test.txt &
sleep 0.5
echo '*** TEST du contenu du fichier testCreation_test.txt'
if grep -Fxq "id incorrect : doit etre un int" ../PrintNoeud/testCreation_test.txt
then
	echo '----------------Test réussi'
else
    echo '----------------Test echec'
fi

# boucle for
for i in {1..11}
do
    echo '*** Creation du noeud '$i' ***'
    java -Djava.security.policy=../mypolicy rmi.tree.SiteImpl $i > ../PrintNoeud/creation_test$i.txt &
    echo '*** Creation du noeud '$i' OK, pid '$!' ***'
    list_pid[$i]=$!
	sleep 0.5
	
	echo '*** TEST du contenu du fichier creation_test'$i'.txt'
    if grep -Fxq "//:1099/"$i ../PrintNoeud/creation_test$i.txt
	then
		echo '----------------Creation du noeud'$i' réussi'
	else
		echo '----------------Creation du noeud'$i' echec'
	fi
done

echo ''


echo '/***************************************/'
echo '/*        Création des aretes          */'
echo '/***************************************/'
echo ''
echo ''
echo '		 1         '
echo '		/  \        '
echo '	       2    3       '
echo '	      / \     \     '
echo '             4   5     9    '
echo '            / \   \   / \  ' 
echo '           6   7   8 10  11'
echo ''
        
echo '*** Creation arete 1 2 ***'
java -Djava.security.policy=../mypolicy rmi.tree.Arete 1 2 > ../PrintNoeud/A1_2_test.txt &
echo '*** Creation arete 1 2 OK, pid '$!' ***'
wait $!

echo '*** TEST du contenu du fichier A1_2_test.txt'
if grep -Fxq "Attribution fini" ../PrintNoeud/A1_2_test.txt
then
	echo '----------------Liaison réussi'
else
    echo '----------------Liaison echec'
fi

echo '*** Creation arete 1 3 ***'
java -Djava.security.policy=../mypolicy rmi.tree.Arete 1 3 > ../PrintNoeud/A1_3_test.txt &
echo '*** Creation arete 1 3 OK, pid '$!' ***'
wait $!

echo '*** Creation arete 2 4 ***'
java -Djava.security.policy=../mypolicy rmi.tree.Arete 2 4 > ../PrintNoeud/A2_4_test.txt &
echo '*** Creation arete 2 4 OK, pid '$!' ***'
wait $!

echo '*** Creation arete 2 5 ***'
java -Djava.security.policy=../mypolicy rmi.tree.Arete 2 5 > ../PrintNoeud/A2_5_test.txt &
echo '*** Creation arete 2 5 OK, pid '$!' ***'
wait $!

echo '*** Creation arete 3 9 ***'
java -Djava.security.policy=../mypolicy rmi.tree.Arete 3 9 > ../PrintNoeud/A3_9_test.txt &
echo '*** Creation arete 3 9 OK, pid '$!' ***'
wait $!

echo '*** Creation arete 4 6 ***'
java -Djava.security.policy=../mypolicy rmi.tree.Arete 4 6 > ../PrintNoeud/A4_6_test.txt &
echo '*** Creation arete 4 6 OK, pid '$!' ***'
wait $!

echo '*** Creation arete 4 7 ***'
java -Djava.security.policy=../mypolicy rmi.tree.Arete 4 7 > ../PrintNoeud/A4_7_test.txt &
echo '*** Creation arete 4 7 OK, pid '$!' ***'
wait $!

echo '*** Creation arete 5 8 ***'
java -Djava.security.policy=../mypolicy rmi.tree.Arete 5 8 > ../PrintNoeud/A5_8_test.txt &
echo '*** Creation arete 5 8 OK, pid '$!' ***'
wait $!

echo '*** Creation arete 9 10 ***'
java -Djava.security.policy=../mypolicy rmi.tree.Arete 9 10 > ../PrintNoeud/A9_10_test.txt &
echo '*** Creation arete 9 10 OK, pid '$!' ***'
wait $!

echo '*** Creation arete 9 11 ***'
java -Djava.security.policy=../mypolicy rmi.tree.Arete 9 11 > ../PrintNoeud/A9_11_test.txt &
echo '*** Creation arete 9 11 OK, pid '$!' ***'
wait $!


echo ''
echo '/***************************************/'
echo '/*        Envoi des messages           */'
echo '/***************************************/'
echo ''

echo '*** Envoi du message "Hello" a partir du noeud 1 ***'
java -Djava.security.policy=../mypolicy rmi.tree.SendMessage 1 hello &
echo '*** Fin de lenvoi du message, pid '$!' ***'
wait $!

# Test que tous les noeuds ont reçut le message
for i in {1..11}
do
    echo '*** TEST du contenu du fichier creation_test'$i'.txt'
	if grep -Fxq "** MESSAGE RECU **" ../PrintNoeud/creation_test$i.txt
	then
		echo '----------------Envoi réussi sur le noeud '$i
	else
		echo '----------------Envoi echec sur le noeud '$i
	fi
done

echo ''
echo "Appuyer sur Entrée pour terminer..."
read a
echo '*** fermeture des java ***'
kill ${list_pid[*]}
echo '*** fermeture du registry ***'
kill $pid_registry